package name.fowler.refactoring.simplestore.reports;

import java.util.List;

public class HtmlFormatter extends Formatter {
	private StringBuilder result;

	@Override
	public void init() {
		result = new StringBuilder();
		result.append("<html><body>\n");
	}
	
	@Override
	public void finish() {
		result.append("</body></html>\n");
	}
	
    @Override
	public void addHeader(String header) {
    	result.append("<h1>").append(header).append("</h1>\n");
	}
    
    @Override
	public void addFooter(String footer) {
    	result.append("<h5>").append(footer).append("</h5>\n");
	}

	@Override
	public void addParagraph(String paragraph) {
		result.append("<p>").append(paragraph).append("</p>\n");
	}

	@Override
	public void addLine(String line) {
		result.append(line);
	}

	@Override
	public void addBreak() {
		result.append("<br/>");
	}

	@Override
	public void addTable(List<List<String>> table) {
		result.append("<table border=\"1\">\n");
        for(List<String> row : table) {
        	result.append("<tr>");
            for(String column : row) {
                result.append("<td>").append(column).append("</td>");
            }
            result.append("</tr>");
        }
        result.append("</table>\n");
	}
    
    public String getResult()
    {
    	return result.toString();
    }
}
